package ExamLab.Test1;

class AA {
	synchronized void doTask1() {
		System.out.println(Thread.currentThread().getName());
		System.out.print(1);
	}
}

class BB extends Thread {

	AA a;
	Thread t;

	BB(AA a, Thread t) {
		this.a = a;
		this.t = t;
	}

	public void run() {
		doTask2();
	}

	public void doTask2() {
		synchronized (a) {
			try {
				System.out.println(Thread.currentThread().getName());
				t.join();
				System.out.println(Thread.currentThread().getName());
			} catch (Exception e) {
			}
		}
	}

	public void doTask3() {
		synchronized (a) {
			System.out.println(Thread.currentThread().getName());
			System.out.print(2);
		}
	}
}

public class TDemo {

	public static void main(String args[]) throws Exception {
		AA a = new AA();
		BB b = new BB(a, Thread.currentThread());
		b.start();

		Thread.sleep(1000);
		a.doTask1();
		b.doTask3();
	}
}